//
//  EFARptSelDateVC.m
//  efairy
//
//  Created by XiangDongMBP on 2017/8/17.
//  Copyright © 2017年 East. All rights reserved.
//

#import "EFARptSelDateVC.h"
#import "NSDate+XDTimerFormatter.h"

@interface EFARptSelDateVC ()<UIPickerViewDelegate, UIPickerViewDataSource>
@property (weak, nonatomic) IBOutlet UIButton *cancelBtn;
@property (weak, nonatomic) IBOutlet UIButton *sureBtn;
@property (weak, nonatomic) IBOutlet UISegmentedControl *typeSegmentCtrl;
@property (weak, nonatomic) IBOutlet UIPickerView *pickerView;
//@property (weak, nonatomic) IBOutlet UIDatePicker *datePicker;

@property (nonatomic, strong) NSArray<NSString *> *year;
@property (nonatomic, strong) NSCalendar *curCalendar;
@property (nonatomic, strong) NSDate *nowDate;
@property (nonatomic, strong) NSDate *startDate;    //!< 起始日期
@property (nonatomic, strong) NSDateComponents *startDateComponents;    //!< 起始Components
@property (nonatomic, strong) NSDate *endDate;      //!< 结束日期
@property (nonatomic, strong) NSDateComponents *endDateComponents;      //!< 结束Components
@property (nonatomic, readonly) EFARptType rptType; //!< 当前报表类型
@end

@implementation EFARptSelDateVC

#pragma mark - life cycle
- (instancetype)init {
    self = [super init];
    if (self) {
        self.modalPresentationStyle = UIModalPresentationOverCurrentContext;
        self.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self initLogicData];
    [self initNavigationBar];
    [self initSubViews];
}
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    //!< 为了防止滚轮出现大于当前的日期
    [self.pickerView reloadAllComponents];
}
- (void)initLogicData {
    
}
- (void)initNavigationBar {
    
}
- (void)initSubViews {
    [self.cancelBtn addTarget:self action:@selector(cancelBtnTapped:) forControlEvents:UIControlEventTouchUpInside];
    [self.sureBtn addTarget:self action:@selector(sureBtnTapped:) forControlEvents:UIControlEventTouchUpInside];
    [self.typeSegmentCtrl addTarget:self action:@selector(segmentValueChange:) forControlEvents:UIControlEventValueChanged];
    // 初始化位置
    [self.typeSegmentCtrl setSelectedSegmentIndex:self.reportTypeInit];
    
    self.pickerView.dataSource = self;
    self.pickerView.delegate = self;
    // 初始化滚轮的位置
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *comInit = [calendar components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitQuarter | NSCalendarUnitWeekOfYear) fromDate:self.selDateInit];
    NSDateComponents *comStart = [calendar components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitQuarter | NSCalendarUnitWeekOfYear) fromDate:self.startDate];
    [self.pickerView selectRow:(comInit.year - comStart.year) inComponent:0 animated:NO];
    if (self.reportTypeInit == EFARptTypeDay) {
        [self.pickerView selectRow:(comInit.month - 1) inComponent:1 animated:NO];
        [self.pickerView selectRow:(comInit.day - 1) inComponent:2 animated:NO];
    } else if (self.reportTypeInit == EFARptTypeWeek) {
        [self.pickerView selectRow:(comInit.weekOfYear - 1) inComponent:1 animated:NO];
    } else if (self.reportTypeInit == EFARptTypeMonth) {
        [self.pickerView selectRow:(comInit.month - 1) inComponent:1 animated:NO];
    } else if (self.reportTypeInit == EFARptTypeQuarter) {
        NSInteger quarter = (comInit.month - 1) / 3 + 1;
        [self.pickerView selectRow:(quarter - 1) inComponent:1 animated:NO];
    } else if (self.reportTypeInit == EFARptTypeYear) {
        ;
    }
//    [self.pickerView reloadAllComponents];
    
//    self.datePicker.minimumDate = self.startDate;
//    self.datePicker.maximumDate = self.endDate;
    
    // 备注
    {
//        // 备注1：这种方式计算出来的date有问题
//        NSDateComponents *components = [[NSDateComponents alloc] init];
//        components.year = 2015;
//        components.weekOfYear = 1;
//        components.weekday = 1;
//        NSDate *date = [self.curCalendar dateFromComponents:components];
//        NSLog(@"2015年第一周的周日 = %@", [date xd_yyyyMMddHHmmss]);
//        
//        // 备注2：这种方式计算出来的Quarter也有问题
//        NSDateComponents *com2 = [self.curCalendar components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitQuarter | NSCalendarUnitWeekOfYear) fromDate:self.endDate];
//        NSLog(@"截至日期的季度 : %ld", com2.quarter);
        
        // 备注3：pickerView的attributeTitle也是坑，没什么用
    }
    // 日
    {
//        for (NSInteger j = 0; j < 1; j++) {
//            NSDate *start2015Date = [self.curCalendar dateWithEra:1 year:2014 + j month:1 day:1 hour:0 minute:0 second:0 nanosecond:0];
//            
//            NSRange range = [self.curCalendar rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitYear forDate:start2015Date];
//            NSDateComponents *com = [[NSDateComponents alloc] init];
//            for (NSInteger i = 0; i < range.length; i++) {
//                com.day = i;
//                NSDate *temp = [self.curCalendar dateByAddingComponents:com toDate:start2015Date options:NSCalendarMatchStrictly];
////                NSDate *startOfWeek;
////                NSTimeInterval length;
////                [self.curCalendar rangeOfUnit:NSCalendarUnitDay
////                                    startDate:&startOfWeek
////                                     interval:&length
////                                      forDate:temp];
////                NSDate *endOfWeek = [startOfWeek dateByAddingTimeInterval:length - 1];
//                NSLog(@"%ld年第%ld天%@", 2014 + j, i+1, [temp xd_yyyy_MM_dd]);
//            }
//        }
    }
    // 周
    {
//        for (NSInteger j = 0; j < 4; j++) {
//            NSDate *start2015Date = [self.curCalendar dateWithEra:1 year:2014 + j month:1 day:1 hour:0 minute:0 second:0 nanosecond:0];
//            
//            NSRange range = [self.curCalendar rangeOfUnit:NSCalendarUnitWeekOfYear inUnit:NSCalendarUnitYear forDate:start2015Date];
//            NSDateComponents *com = [[NSDateComponents alloc] init];
//            for (NSInteger i = 0; i < range.length; i++) {
//                com.day = 7*i;
//                NSDate *temp = [self.curCalendar dateByAddingComponents:com toDate:start2015Date options:NSCalendarMatchStrictly];
//                
//                NSDate *startOfWeek;
//                NSTimeInterval length;
//                [self.curCalendar rangeOfUnit:NSCalendarUnitWeekOfYear
//                                    startDate:&startOfWeek
//                                     interval:&length
//                                      forDate:temp];
//                NSDate *endOfWeek = [startOfWeek dateByAddingTimeInterval:length - 1];
//                NSLog(@"%ld年第%ld周(%@ - %@)", 2014 + j, i+1, [startOfWeek xd_yyyy_MM_dd], [endOfWeek xd_yyyy_MM_dd]);
//            }
//        }
    }
    // 月
    {
//        for (NSInteger j = 0; j < 4; j++) {
//            NSDate *start2015Date = [self.curCalendar dateWithEra:1 year:2014 + j month:1 day:1 hour:0 minute:0 second:0 nanosecond:0];
//            
//            NSRange range = [self.curCalendar rangeOfUnit:NSCalendarUnitMonth inUnit:NSCalendarUnitYear forDate:start2015Date];
//            NSDateComponents *com = [[NSDateComponents alloc] init];
//            for (NSInteger i = 0; i < range.length; i++) {
//                com.month = i;
//                NSDate *temp = [self.curCalendar dateByAddingComponents:com toDate:start2015Date options:NSCalendarMatchStrictly];
//                
//                NSDate *startOfWeek;
//                NSTimeInterval length;
//                [self.curCalendar rangeOfUnit:NSCalendarUnitMonth
//                                    startDate:&startOfWeek
//                                     interval:&length
//                                      forDate:temp];
//                NSDate *endOfWeek = [startOfWeek dateByAddingTimeInterval:length - 1];
//                NSLog(@"%ld年%ld月(%@ - %@)", 2014 + j, i+1, [startOfWeek xd_yyyy_MM_dd], [endOfWeek xd_yyyy_MM_dd]);
//            }
//        }
    }
    // 季度
    {
//        for (NSInteger j = 0; j < 4; j++) {
//            NSDate *start2015Date = [self.curCalendar dateWithEra:1 year:2014 + j month:1 day:1 hour:0 minute:0 second:0 nanosecond:0];
//            
//            NSRange range = [self.curCalendar rangeOfUnit:NSCalendarUnitQuarter inUnit:NSCalendarUnitYear forDate:start2015Date];
//            NSDateComponents *com = [[NSDateComponents alloc] init];
//            for (NSInteger i = 0; i < range.length; i++) {
//                com.month = 3*i;
//                NSDate *temp = [self.curCalendar dateByAddingComponents:com toDate:start2015Date options:NSCalendarMatchStrictly];
//                
//                NSDate *startOfWeek;
//                NSTimeInterval length;
//                [self.curCalendar rangeOfUnit:NSCalendarUnitQuarter
//                                    startDate:&startOfWeek
//                                     interval:&length
//                                      forDate:temp];
//                NSDate *endOfWeek = [startOfWeek dateByAddingTimeInterval:length - 1];
//                NSLog(@"%ld年%ld季度(%@ - %@)", 2014 + j, i+1, [startOfWeek xd_yyyy_MM_dd], [endOfWeek xd_yyyy_MM_dd]);
//            }
//        }
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark - UIPickerViewDataSource
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    NSArray<NSNumber *> *array = @[@(3), @(2), @(2), @(2), @(1)];
    return [array[self.rptType] integerValue];
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    // 日
    if (self.rptType == EFARptTypeDay) {
        if (component == 0) {           //年
            return self.endDateComponents.year - self.startDateComponents.year + 1;
        } else if (component == 1) {    //月
            NSInteger year = self.startDateComponents.year + [pickerView selectedRowInComponent:0];
            NSDate *date = [self.curCalendar dateWithEra:1 year:year month:1 day:1 hour:0 minute:0 second:0 nanosecond:0];
            NSRange range = [self.curCalendar rangeOfUnit:NSCalendarUnitMonth inUnit:NSCalendarUnitYear forDate:date];
            return (year < self.endDateComponents.year) ? range.length : self.endDateComponents.month;
        } else if (component == 2) {    //日
            NSInteger year = self.startDateComponents.year + [pickerView selectedRowInComponent:0];
            NSInteger month = 1 + [pickerView selectedRowInComponent:1];
            NSDate *date = [self.curCalendar dateWithEra:1 year:year month:month day:1 hour:0 minute:0 second:0 nanosecond:0];
            NSRange range = [self.curCalendar rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:date];
            if (year < self.endDateComponents.year) {
                return range.length;
            } else {
                return (month < self.endDateComponents.month) ? range.length : self.endDateComponents.day;
            }
        }
    } else if (self.rptType == EFARptTypeWeek) {
        if (component == 0) {        // 年(考虑到年初第一周的特殊情况)
            return self.endDateComponents.year - self.startDateComponents.year + ((self.endDateComponents.weekOfYear - 1) ? 1 : 0);
        } else if (component == 1) { // 周
            NSInteger year = self.startDateComponents.year + [pickerView selectedRowInComponent:0];
            NSDate *date = [self.curCalendar dateWithEra:1 year:year month:1 day:1 hour:0 minute:0 second:0 nanosecond:0];
            NSRange range = [self.curCalendar rangeOfUnit:NSCalendarUnitWeekOfYear inUnit:NSCalendarUnitYear forDate:date];
            return (year < self.endDateComponents.year) ? range.length : self.endDateComponents.weekOfYear - 1;
        }
    } else if (self.rptType == EFARptTypeMonth) {
        if (component == 0) {// 年(考虑到年初第一周的特殊情况)
            return self.endDateComponents.year - self.startDateComponents.year + ((self.endDateComponents.month - 1) ? 1 : 0);
        } else if (component == 1) { // 月
            NSInteger year = self.startDateComponents.year + [pickerView selectedRowInComponent:0];
            NSDate *date = [self.curCalendar dateWithEra:1 year:year month:1 day:1 hour:0 minute:0 second:0 nanosecond:0];
            NSRange range = [self.curCalendar rangeOfUnit:NSCalendarUnitMonth inUnit:NSCalendarUnitYear forDate:date];
            return (year < self.endDateComponents.year) ? range.length : self.endDateComponents.month - 1;
        }
    } else if (self.rptType == EFARptTypeQuarter) {
        if (component == 0) {// 年(考虑到年初第一季度的特殊情况)
            NSInteger endQuarter = (self.endDateComponents.month - 1) / 3 + 1;
            return self.endDateComponents.year - self.startDateComponents.year + ((endQuarter - 1) ? 1 : 0);
        } else if (component == 1) { // 季度
            NSInteger year = self.startDateComponents.year + [pickerView selectedRowInComponent:0];
            NSDate *date = [self.curCalendar dateWithEra:1 year:year month:1 day:1 hour:0 minute:0 second:0 nanosecond:0];
            NSRange range = [self.curCalendar rangeOfUnit:NSCalendarUnitQuarter inUnit:NSCalendarUnitYear forDate:date];
            NSInteger endQuarter = (self.endDateComponents.month - 1) / 3 + 1;
//            NSLog(@"%ld年季度数量：%ld", year, (year < self.endDateComponents.year) ? range.length : endQuarter - 1);
            return (year < self.endDateComponents.year) ? range.length : endQuarter - 1;
        }
    } else if (self.rptType == EFARptTypeYear) {
        if (component == 0) {
            return self.endDateComponents.year - self.startDateComponents.year;
        }
    }
    
    return 0;
}
#pragma mark - UIPickerViewDelegate
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
    NSArray<NSNumber *> *array = @[@(3), @(2), @(2), @(2), @(1)];
    if (self.rptType == EFARptTypeWeek) {
        return component == 0 ? CGRectGetWidth(pickerView.bounds) / 3.0 : CGRectGetWidth(pickerView.bounds) * 2.0 / 3.0;
    } else {
        return CGRectGetWidth(pickerView.bounds) / [array[self.rptType] integerValue];
    }
}

/*
//- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
//    if (self.rptType == EFARptTypeDay || self.rptType == EFARptTypeMonth || self.rptType == EFARptTypeYear) {
//        if (component == 0) {
//            NSInteger year = self.startDateComponents.year + row;
//            return [NSString stringWithFormat:@"%ld", year];
//        } else if (component == 1) {
////            NSInteger year = self.startDateComponents.year + [pickerView selectedRowInComponent:0];
//            NSInteger month = 1 + row;
//            return [NSString stringWithFormat:@"%ld", month];
//        } else if(component == 2) {
////            NSInteger year = self.startDateComponents.year + [pickerView selectedRowInComponent:0];
////            NSInteger month = 1 + [pickerView selectedRowInComponent:1];
//            NSInteger day = 1 + row;
//            return [NSString stringWithFormat:@"%ld", day];
//        }
//    } else if (self.rptType == EFARptTypeWeek) {
//        if (component == 0) {           // 年
//            NSInteger year = self.startDateComponents.year + row;
//            return [NSString stringWithFormat:@"%ld", year];
//        } else if (component == 1) {    // 周
//            NSInteger year = self.startDateComponents.year + [pickerView selectedRowInComponent:0];
//            NSInteger week = 1 + row;
//            NSDate *date = [self.curCalendar dateWithEra:1 year:year month:1 day:1 hour:0 minute:0 second:0 nanosecond:0];
//            NSDateComponents *com = [[NSDateComponents alloc] init];
//            com.day = 7 * row;
//            NSDate *temp = [self.curCalendar dateByAddingComponents:com toDate:date options:NSCalendarMatchStrictly];
//            NSDate *startOfWeek; NSTimeInterval length;
//            [self.curCalendar rangeOfUnit:NSCalendarUnitWeekOfYear startDate:&startOfWeek interval:&length forDate:temp];
//            NSDate *endOfWeek = [startOfWeek dateByAddingTimeInterval:length - 1];
//            return [NSString stringWithFormat:@"第%ld周(%@-%@)", week, [startOfWeek xd_MM_dd], [endOfWeek xd_MM_dd]];
//        }
//    } else if (self.rptType == EFARptTypeQuarter) {
//        if (component == 0) {
//            NSInteger year = self.startDateComponents.year + row;
//            return [NSString stringWithFormat:@"%ld", year];
//        } else if (component == 1) {
//            //            NSInteger year = self.startDateComponents.year + [pickerView selectedRowInComponent:0];
//            NSInteger quarter = 1 + row;
//            return [NSString stringWithFormat:@"%ld季度", quarter];
//        }
//    }
//    return @"0";
//}
*/
/*
//- (NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component {
//    NSString *title = @"";
//    if (self.rptType == EFARptTypeDay || self.rptType == EFARptTypeMonth || self.rptType == EFARptTypeYear) {
//        if (component == 0) {
//            NSInteger year = self.startDateComponents.year + row;
//            title = [NSString stringWithFormat:@"%ld", year];
//        } else if (component == 1) {
//            NSInteger month = 1 + row;
//            title = [NSString stringWithFormat:@"%ld", month];
//        } else if(component == 2) {
//            NSInteger day = 1 + row;
//            title = [NSString stringWithFormat:@"%ld", day];
//        }
//    } else if (self.rptType == EFARptTypeWeek) {
//        if (component == 0) {           // 年
//            NSInteger year = self.startDateComponents.year + row;
//            title = [NSString stringWithFormat:@"%ld", year];
//        } else if (component == 1) {    // 周
//            NSInteger year = self.startDateComponents.year + [pickerView selectedRowInComponent:0];
//            NSInteger week = 1 + row;
//            NSDate *date = [self.curCalendar dateWithEra:1 year:year month:1 day:1 hour:0 minute:0 second:0 nanosecond:0];
//            NSDateComponents *com = [[NSDateComponents alloc] init];
//            com.day = 7 * row;
//            NSDate *temp = [self.curCalendar dateByAddingComponents:com toDate:date options:NSCalendarMatchStrictly];
//            NSDate *startOfWeek; NSTimeInterval length;
//            [self.curCalendar rangeOfUnit:NSCalendarUnitWeekOfYear startDate:&startOfWeek interval:&length forDate:temp];
//            NSDate *endOfWeek = [startOfWeek dateByAddingTimeInterval:length - 1];
//            title = [NSString stringWithFormat:@"第%ld周(%@-%@)", week, [startOfWeek xd_MM_dd], [endOfWeek xd_MM_dd]];
//        }
//    } else if (self.rptType == EFARptTypeQuarter) {
//        if (component == 0) {
//            NSInteger year = self.startDateComponents.year + row;
//            title = [NSString stringWithFormat:@"%ld", year];
//        } else if (component == 1) {
//            NSInteger quarter = 1 + row;
//            title = [NSString stringWithFormat:@"%ld季度", quarter];
//        }
//    }
//    NSDictionary * dic = @{NSForegroundColorAttributeName: [UIColor blueColor],
//                           NSFontAttributeName: [UIFont systemFontOfSize:10]};
//    return [[NSAttributedString alloc] initWithString:title attributes:dic];
//}
*/

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
    UILabel* tView = (UILabel*)view;
    if (!tView){
        tView = [[UILabel alloc] init];
        tView.font = [UIFont systemFontOfSize:14.0];
        tView.textAlignment = NSTextAlignmentCenter;
    }
    // Fill the label text here
    NSString *title = @"";
    if (self.rptType == EFARptTypeDay || self.rptType == EFARptTypeMonth || self.rptType == EFARptTypeYear) {
        if (component == 0) {
            NSInteger year = self.startDateComponents.year + row;
            title = [NSString stringWithFormat:@"%ld", (long)year];
        } else if (component == 1) {
            NSInteger month = 1 + row;
            title = [NSString stringWithFormat:@"%ld", (long)month];
        } else if(component == 2) {
            NSInteger day = 1 + row;
            title = [NSString stringWithFormat:@"%ld", (long)day];
        }
    } else if (self.rptType == EFARptTypeWeek) {
        if (component == 0) {           // 年
            NSInteger year = self.startDateComponents.year + row;
            title = [NSString stringWithFormat:@"%ld", (long)year];
        } else if (component == 1) {    // 周
            NSInteger year = self.startDateComponents.year + [pickerView selectedRowInComponent:0];
            NSInteger week = 1 + row;
            NSDate *date = [self.curCalendar dateWithEra:1 year:year month:1 day:1 hour:0 minute:0 second:0 nanosecond:0];
            NSDateComponents *com = [[NSDateComponents alloc] init];
            com.day = 7 * row;
            NSDate *temp = [self.curCalendar dateByAddingComponents:com toDate:date options:NSCalendarMatchStrictly];
            NSDate *startOfWeek; NSTimeInterval length;
            [self.curCalendar rangeOfUnit:NSCalendarUnitWeekOfYear startDate:&startOfWeek interval:&length forDate:temp];
            NSDate *endOfWeek = [startOfWeek dateByAddingTimeInterval:length - 1];
            title = [NSString stringWithFormat:@"第%ld周(%@-%@)", (long)week, [startOfWeek xd_MM_dd], [endOfWeek xd_MM_dd]];
        }
    } else if (self.rptType == EFARptTypeQuarter) {
        if (component == 0) {
            NSInteger year = self.startDateComponents.year + row;
            title = [NSString stringWithFormat:@"%ld", (long)year];
        } else if (component == 1) {
            NSInteger quarter = 1 + row;
            title = [NSString stringWithFormat:@"%ld季度", (long)quarter];
        }
    }
    
    tView.text = title;
    return tView;
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    if (self.rptType == EFARptTypeDay) {
        if (component == 0) {
            [pickerView reloadComponent:1];
            [pickerView reloadComponent:2];
        } else if (component == 1) {
            [pickerView reloadComponent:2];
        }
    } else if (self.rptType == EFARptTypeWeek || self.rptType == EFARptTypeMonth || self.rptType == EFARptTypeQuarter) {
        if (component == 0) {
            [pickerView reloadComponent:1];
        }
    }
}

#pragma mark - event response
- (void)cancelBtnTapped:(UIButton *)btn {
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (void)segmentValueChange:(UISegmentedControl *)segment {
    [self.pickerView setNeedsLayout];   // pickerView更改布局width有问题，所以需要这里手动让其重新布局
    
    [self.pickerView reloadAllComponents];
    NSArray<NSNumber *> *array = @[@(3), @(2), @(2), @(2), @(1)];
    NSInteger num = [array[self.rptType] integerValue];
    for (NSInteger i = 0; i < num; i++) {
        [self.pickerView selectRow:0 inComponent:i animated:YES];
    }
}
- (void)sureBtnTapped:(UIButton *)btn {
    NSDate *resultStartDate = nil;
    NSDate *resultEndDate = nil;
    NSString * resultStr = nil;
    NSInteger year  = self.startDateComponents.year + [self validIndexOfComponents:0];
    if (self.rptType == EFARptTypeDay) {
        NSInteger month = 1 + [self validIndexOfComponents:1];
        NSInteger day = 1 + [self validIndexOfComponents:2];
        // 校验数据的合法性
        resultStartDate = [self.curCalendar dateWithEra:1 year:year month:month day:day hour:0 minute:0 second:0 nanosecond:0];
        resultEndDate = [self.curCalendar dateWithEra:1 year:year month:month day:day hour:23 minute:59 second:59 nanosecond:0];
        resultStr = [resultStartDate xd_yyyy_MM_dd];
    } else if (self.rptType == EFARptTypeWeek) {
        NSInteger week = 1 + [self validIndexOfComponents:1];
        NSDate *year1Date = [self.curCalendar dateWithEra:1 year:year month:1 day:1 hour:0 minute:0 second:0 nanosecond:0];
        NSDateComponents *com = [[NSDateComponents alloc] init];
        com.day = 7 * (week - 1);
        NSDate *temp = [self.curCalendar dateByAddingComponents:com toDate:year1Date options:NSCalendarMatchStrictly];
        NSDate *startOfWeek; NSTimeInterval length;
        [self.curCalendar rangeOfUnit:NSCalendarUnitWeekOfYear startDate:&startOfWeek interval:&length forDate:temp];
        NSDate *endOfWeek = [startOfWeek dateByAddingTimeInterval:length - 1];
        resultStartDate = startOfWeek;
        resultEndDate = endOfWeek;
        resultStr = [NSString stringWithFormat:@"%ld年第%ld周(%@-%@)", (long)year, (long)week, [startOfWeek xd_MM_dd], [endOfWeek xd_MM_dd]];
    } else if (self.rptType == EFARptTypeMonth) {
        NSInteger month = 1 + [self validIndexOfComponents:1];
        NSDate *temp = [self.curCalendar dateWithEra:1 year:year month:month day:1 hour:0 minute:0 second:0 nanosecond:0];
        NSDate *startOfMonth; NSTimeInterval length;
        [self.curCalendar rangeOfUnit:NSCalendarUnitMonth startDate:&startOfMonth interval:&length forDate:temp];
        NSDate *endOfMonth = [startOfMonth dateByAddingTimeInterval:length - 1];
        resultStartDate = startOfMonth;
        resultEndDate = endOfMonth;
        resultStr = [resultStartDate xd_yyyy_MM];
    } else if (self.rptType == EFARptTypeQuarter) {
        NSInteger quarter = 1 + [self validIndexOfComponents:1];
        NSInteger month = quarter * 3;
        NSDate *temp = [self.curCalendar dateWithEra:1 year:year month:month day:1 hour:0 minute:0 second:0 nanosecond:0];
        NSDate *startOfQuarter; NSTimeInterval length;
        [self.curCalendar rangeOfUnit:NSCalendarUnitQuarter startDate:&startOfQuarter interval:&length forDate:temp];
        NSDate *endOfQuarter = [startOfQuarter dateByAddingTimeInterval:length - 1];
        resultStartDate = startOfQuarter;
        resultEndDate = endOfQuarter;
        resultStr = [NSString stringWithFormat:@"%ld年%ld季度", (long)year, (long)quarter];
    } else if (self.rptType == EFARptTypeYear) {
        NSDate *temp = [self.curCalendar dateWithEra:1 year:year month:1 day:1 hour:0 minute:0 second:0 nanosecond:0];
        NSDate *startOfYear; NSTimeInterval length;
        [self.curCalendar rangeOfUnit:NSCalendarUnitYear startDate:&startOfYear interval:&length forDate:temp];
        NSDate *endOfYear = [startOfYear dateByAddingTimeInterval:length - 1];
        resultStartDate = startOfYear;
        resultEndDate = endOfYear;
        resultStr = [NSString stringWithFormat:@"%ld年", (long)year];
    }
    
    [self dismissViewControllerAnimated:YES completion:^{
        if (self.successHandle) {
            self.successHandle(self.rptType, resultStartDate, resultEndDate, resultStr);
            self.successHandle = nil;
        }
    }];
}

#pragma mark - private methods
- (NSInteger)validIndexOfComponents:(NSInteger)component {
    NSInteger index = [self.pickerView selectedRowInComponent:component];
    NSInteger num = [self pickerView:self.pickerView numberOfRowsInComponent:component];
    return index < num ? index : 0;
}

#pragma mark - network request
#pragma mark - getters and setters
- (NSDate *)startDate {
    if (!_startDate) {
        // 获取3年前的1月1号时间
        NSInteger year = [self.curCalendar component:NSCalendarUnitYear fromDate:self.nowDate];
        _startDate = [self.curCalendar dateWithEra:1 year:year-3 month:1 day:1 hour:0 minute:0 second:0 nanosecond:0];
    }
    return _startDate;
}
- (NSDateComponents *)startDateComponents {
    if (!_startDateComponents) {
        _startDateComponents = [self.curCalendar components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitQuarter | NSCalendarUnitWeekOfYear) fromDate:self.startDate];
    }
    return _startDateComponents;
}
- (NSDate *)endDate {
    if (!_endDate) {
        // 获取昨天的时间
        NSDateComponents *components = [[NSDateComponents alloc] init];
        components.day = -1;
        _endDate = [self.curCalendar dateByAddingComponents:components toDate:self.nowDate options:NSCalendarMatchStrictly];
    }
    return _endDate;
}
- (NSDateComponents *)endDateComponents {
    if (!_endDateComponents) {
        _endDateComponents = [self.curCalendar components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitQuarter | NSCalendarUnitWeekOfYear) fromDate:self.endDate];
    }
    return _endDateComponents;
}
- (NSCalendar *)curCalendar {
    if (!_curCalendar) {
//        _curCalendar = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian];
        _curCalendar = [NSCalendar currentCalendar];
    }
    return _curCalendar;
}
- (NSDate *)nowDate {
    if (!_nowDate) {
        _nowDate = [NSDate date];
    }
    return _nowDate;
}
- (EFARptType)rptType {
    return [self.typeSegmentCtrl selectedSegmentIndex];
}
@end
